package com.huawei.codearts.controller;

import com.huawei.codearts.service.CountryService;
import com.huawei.codearts.tools.utils.ResultResponse;
import com.huawei.codearts.tools.utils.ResultUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * hibernate 模式 增删改查
 */

@Slf4j
@ResponseBody
@RestController
@RequestMapping(value = "country")
public class CountryController {

    private final CountryService countryService;

    public CountryController(CountryService countryService) {
        this.countryService = countryService;
    }

    @RequestMapping(value = "getOne")
    public ResultResponse<Object> getOne(@RequestParam Map<String, Object> params) {
        // @RequestBody 接收json参数
        // @RequestParam 接收get或post参数, 如果get和post同时有参数并且参数名称相同, 则get参数会覆盖post参数
        System.out.println(params);
        System.out.println(params.get("id"));
        int id = Integer.parseInt((String)params.get("id"));
        return ResultUtils.success(countryService.getOne(id));
    }

    @RequestMapping(value = "getAll")
    public ResultResponse<Object> getAll(@RequestParam Map<String, Object> params) {
        return ResultUtils.success(countryService.getAll());
    }

    @RequestMapping(value = "getList")
    public ResultResponse<Object> getList(@RequestParam Map<String, Object> params) {
        return ResultUtils.success(countryService.getList());
    }

    @RequestMapping(value = "add")
    public ResultResponse<Object> insert(@RequestParam Map<String, Object> params) {
        Map<String, Object> save = countryService.save();
        return ResultUtils.response((Integer) save.get("code"), (String) save.get("msg"), save.get("data"));
    }

    @RequestMapping(value = "update")
    public ResultResponse<Object> update(@RequestParam Map<String, Object> params) {
        int id = Integer.parseInt((String)params.get("id"));
        return ResultUtils.success(countryService.update(id));
    }

    @RequestMapping(value = "del")
    public ResultResponse<Object> del(@RequestParam Map<String, Object> params) {
        int id = Integer.parseInt((String)params.get("id"));
        return ResultUtils.success(countryService.del(id));
    }

}
